#include "common.h"
#include "CassiopeiaMain.h"

//replacing (with regex):
//find: Translate\({[a-zA-Z_\(\)"]*}\)\.c_str\(\)
//replace: \1		


//Enable Visual Styles
#pragma comment(linker,"/manifestdependency:\"type='win32' name='Microsoft.Windows.Common-Controls' \
version='6.0.0.0' processorArchitecture='*' publicKeyToken='6595b64144ccf1df' language='*'\"")

int APIENTRY _tWinMain(HINSTANCE hInstance,	HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nShowCmd)
{
	tstring deskNotesTitle = TEXT("Notes");
	bool deskNotesOnly = false;

	//Show Debug Console

	#if _DEBUG
		AllocConsole();
		AttachConsole(GetCurrentProcessId());
		freopen_s((FILE**)stdout,"CON", "w", stdout);
	#endif

	//disable snap animation for hWnd
	/*HWND h = FindWindow(TEXT("Notepad"),0);
	BOOL b=TRUE;
	DwmSetWindowAttribute(h,
						DWMWA_TRANSITIONS_FORCEDISABLED,
						&b,
						sizeof(DWMWA_TRANSITIONS_FORCEDISABLED));*/


	//allow only one instance of Cassiopeia
	HANDLE hMutex = CreateMutex(0, FALSE, TEXT("{40035345-53E7-43b2-AEFB-D218A3E0B294}"));
	if (hMutex && GetLastError() == ERROR_ALREADY_EXISTS)
	{
		ReleaseMutex(hMutex);

		CloseHandle(hMutex);
		hMutex = 0;

		deskNotesOnly = true;
	}
	ReleaseMutex(hMutex);


	//evaluate command arguments
	if (__argc > 1 && (!_tcscmp(_tcslwr(__targv[1]), TEXT("desknotesonly")) || !_tcscmp(_tcslwr(__targv[1]), TEXT("-d"))))
	{
		deskNotesOnly = true;
		
		if (hMutex)
			CloseHandle(hMutex);

		if (__argc > 2)
			deskNotesTitle = __targv[2];
	}


	//second mutex for deskNotesOnly mode (executable path without filename)
	TCHAR exePath[_MAX_PATH] = {0};
	TCHAR exeDrive[_MAX_DRIVE] = {0};
	TCHAR exeDir[_MAX_DIR]={0};

	GetModuleFileName(0, exePath, _MAX_PATH);

	_tsplitpath(exePath, exeDrive, exeDir, 0, 0);
	_tmakepath(exePath, exeDrive, exeDir, 0, 0);

	//CreateMutex doesn't allow '\' !
	for(int i = 0; i < MAX_PATH; i++)
	{
		if(!exePath[i])
			break;

		if(exePath[i] == '\\')
			exePath[i] = '#';
	};

	HANDLE hMutex2 = CreateMutex(0, FALSE, exePath);
	if (hMutex2 && GetLastError() == ERROR_ALREADY_EXISTS)
	{
		ReleaseMutex(hMutex2);

		CloseHandle(hMutex2);
		hMutex2 = 0;
		return 0;
	}
	ReleaseMutex(hMutex2);


	//initialize gdi+
	ULONG_PTR gdiplusToken;  
	GdiplusStartupInput gdiplusStartupInput;
	GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, 0);

	//manipulate working directory, so that it is always the same directory as the executable
	TCHAR path[_MAX_PATH]={0};
	TCHAR drive[_MAX_DRIVE]={0};
	TCHAR dir[_MAX_DIR]={0};
	TCHAR fileName[_MAX_FNAME]={0};
	TCHAR ext[_MAX_EXT]={0};

	GetModuleFileName(0, path, _MAX_PATH);

	_tsplitpath(path,drive,dir,fileName,ext);
	_tmakepath(path,drive, dir, 0,0);

	SetCurrentDirectory(path);




	CassiopeiaMain *cm = 0;
	DeskNotes *dn = 0;

	if (deskNotesOnly)
	{
		dn = new DeskNotes(false, deskNotesTitle);
		dn->Create(hInstance);	
		dn->Activate();	
	}
	else
	{
		cm = CassiopeiaMain::CreateInstance(hInstance);	
	}

	MSG msg;
	while(GetMessage(&msg,0,0,0))
	{
		TranslateMessage(&msg);
		DispatchMessage(&msg);
	}

	if (deskNotesOnly)
		delete dn;
	else
		delete cm;

	//shutdown gdi+
	GdiplusShutdown(gdiplusToken);

	return msg.wParam;
}
